$(function() {
	$(".dept_select").attr("selected","selected");
	$(".type_select").attr("selected","selected");
	
	var dept_id,dept_name,position_id,position_name,position_type,page_num,position_maxmember;
	
	//用户点击搜索按钮
	$(".position_search_btn").click(function(){
		close_alert();
		
		dept_id = $("#dept_id-in-searchForm").val();
		dept_name = $("#search_dept_name_select").val();
		position_id = $("#position_id-in-searchForm").val();
		position_name = $("#position_name-in-searchForm").val();
		position_type = $("#search_position_type_select").val();
		page_num = $(".page_num").val();
		
		ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,page_num,true);
	});
	
	//发送异步搜索请求
	var ajax_position_search = function(dept_id,dept_name,position_id,position_name,position_type,page_num,refresh){
		$.ajax({
			// 发送方法
			type:"GET",
			// 发送url
			url:"fake/position-management/table.JSON",
//			data:{
//			"service":"searchDrs",
//			"dept_id":dept_id,
//			"dept_name":dept_name,
//			"position_id":position_id,
//			"position_name":position_name,
//			"position_type":position_type,
//			"page_num":page_num
//			},
			// 成果返回后
			success: function(response) {
				var jsonData = eval("("+response+")");
				if(jsonData.total_num == "0"){
					alert_div($(".alert_container"),"没有岗位信息");
					$(".position_search_container").html("");
				}
				else{
					if(refresh){
						close_alert();
					}
					ajax_load_position_search(response);
				}
			}
		});
	}
	
	//加载日报搜索列表
	var ajax_load_position_search = function(position_list_data){
		$.ajax({
			type:"GET",
			url:"vmresult/position/position-search.html",
			success: function(html) {
				//console
				console.log(position_list_data);
				$(".position_search_container").html("");
				$(".position_search_container").html(html);
				var jsonData = eval("("+position_list_data+")");
				//获取列表显示
				var listHtml = getSearchList(jsonData.tableRow);
				$(".position_search_body").replaceWith(listHtml);
				//设置table foot
				$(".page_num").text(jsonData.page_num);
				$(".total_page").text(jsonData.total_page);
				$(".total_num").text(jsonData.total_num);
				//点击首页按钮
				$(".first_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					if(page_num == 1)
						alert("已经是首页");
					else
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,1);
				});
				//点击前一页按钮
				$(".pre_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					if(page_num == 1)
						alert("已经是首页");
					else
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,page_num-1);
				});
				//点击下一页按钮
				$(".next_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					var total_page = jsonData.total_page;
					if(page_num == total_page)
						alert("没有更多页面");
					else
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,page_num+1);
				});
				//点击尾页按钮
				$(".last_page_btn").click(function(){
					close_alert();
					
					var page_num = jsonData.page_num;
					var total_page = jsonData.total_page;
					if(page_num == total_page)
						alert("没有更多页面");
					else
						ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,jsonData.total_page);
				});
				//点击添加
				$(".position_create_btn").click(function(){
					close_alert();
					
					$("#add_modal").modal('toggle');
					ajax_add_position();
				});
				//点击查看岗位下员工
				$(".position_view_emp_btn").on("click",function(){
					close_alert();
					
					var position_id = $(this).attr("position_id");
					
					$("#view_emp_modal").modal('toggle');
					
					ajax_emp_search(position_id,page_num,false);
				});
				//点击修改
				$(".position_update_btn").on("click",function(){
					close_alert();
					
					var position_id_ = $(this).attr("position_id");					
					$("#edit_modal").modal('toggle'); 
					
					ajax_update_position();
				});
				//点击删除
				$(".position_delete_btn").on("click",function(){
					close_alert();
					
					var position_id = $(this).attr("position_id");
					$("#delete_modal").modal('toggle');
					
					$(".confirm_in_delete").click(function(){
						$.ajax({
							type:"GET",
							url:"fake/position-management/message-del.JSON",
//							data:{
//								"service":"deleteDr",
//								"rep_id":dr_id
//							},
							// 成果返回后
							success: function(response) {
								var jsonData = eval("("+response+")");
								if(jsonData.messageType=="success"){
									alert_div($(".alert_container"),jsonData.message);
									ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,position_maxmember,page_num,false);
								}
								else 
									alert_div($(".alert_container"),jsonData.message);
							}
						});
					
					});
				});
				
			}
		});

	}
	
	var getSearchList = function(list){
		var html = "<tbody class='position_search_body'> ";
						for(var i=0;i<list.length;i++){
							var temp_data = list[i];
							html+="<tr data-ng-repeat='row in tableRow'>" +
										
										"<td>"+temp_data.position_id+"</td>" +
										"<td>"+temp_data.position_name+"</td>" +
										"<td>"+temp_data.dept_name+"</td>" +
										"<td>"+temp_data.position_type+"</td>" +
										"<td>"+temp_data.position_maxmember+"</td>" +
										"<td>" +
											"<div class='hidden-sm hidden-xs btn-group'>" +
													"<button class='btn btn-xs btn-success position_view_emp_btn' position_id='"+temp_data.position_id+"'>" +
														"<i class='ace-icon fa fa-search-plus bigger-120'></i>" +
													"</button>" +
													"<button class='btn btn-xs btn-info position_update_btn' position_id='"+temp_data.position_id+"'>" +
														"<i class='ace-icon fa fa-pencil bigger-120'></i>" +
													"</button>" +
													"<button class='btn btn-xs btn-danger position_delete_btn' position_id='"+temp_data.position_id+"'>" +
														"<i class='ace-icon fa fa-trash-o bigger-120'></i>" +
													"</button>" +
											"</div>" +
										"</td>" +
									"</tr>";
						}
		html+="</tbody>";
		return html;
	}
	
	var ajax_add_position = function(){
		
		$("#add_position_form").validate({
			rules : {
				add_position_id:"required",
				add_position_name:"required",
				add_position_type:"required",
				add_dept_name:"required"
			},
			messages : {
				add_position_id:"岗位编号不能为空",
				add_position_name:"岗位名称不能为空",
				add_position_type:"岗位类型不能为空",
				add_dept_name:"部门名称不能为空"
			},
			submitHandler:function(form){
					$.ajax({
						type:"GET",
						url:"fake/position-management/message-add.JSON",
//						data:{
//							"service":"updateDr",
//							"rep_id":dr_id,
//							"date":dr_date,
//							"work_time":dr_work_time,
//							"over_time_hour":dr_ot_time,
//							"plan":dr_plan,
//							"proj_id":dr_proj,
//							"prp_id":dr_prp,
//							"task":dr_task,
//							"state":dr_state
//						},
						success: function(data) {
							var jsonData = eval("("+data+")");
							//获取填入的数据
							var position_name = $(".add_name_inupt").val();
							var position_type = $(".add_type_input").val();
							var position_maxmember = $(".add_maxmember_input").val();
							var dept_name = $(".add_dept_name_input").val();
							if(jsonData.messageType == "success"){
								alert_div($(".alert_container"),jsonData.message);
								ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,position_maxmember,page_num,false);
							}
							else
								alert_div($(".alert_container"),jsonData.message);
							$(".modal-content").css("display", "none");
							$(".holyContainer").css("opacity", 1);
						}
					});
					
			}   
		});
			
	}

	var ajax_update_position = function(){
		$.ajax({
			type:"GET",
			url:"fake/position-management/table-modify.JSON",
			success: function(page_data) {
				//添加显示数据
				var jsonData = eval("("+page_data+")");
				$(".update_id_input").val(jsonData.position_id);
				$(".update_name_input").val(jsonData.position_name);
				$(".update_type_input").val(jsonData.position_type);
				$(".update_maxmember_input").val(jsonData.position_maxmember);
				$(".update_dept_name_input").val(jsonData.dept_name);

				//提交更新
				var update = false;
				var position_id = $(".update_id_input").val();
				var position_name = $(".update_name_input").val();
				var position_type = $(".update_type_input").val();
				var position_maxmember = $(".update_maxmember_input").val();
				var dept_name = $(".update_dept_name_input").val();
				
				$("#update_position_form").validate({
					rules : {
						update_position_id:"required",
						update_position_name:"required",
						update_position_type:"required",
						update_dept_name:"required"
					},
					messages : {
						update_position_id:"岗位编号不能为空",
						update_position_name:"岗位名称不能为空",
						update_position_type:"岗位类型不能为空",
						update_dept_name:"部门名称不能为空"
					},
					submitHandler:function(form){
							$.ajax({
								type:"GET",
								url:"fake/position-management/message-modify.JSON",
//								data:{
//									"service":"updateDr",
//									"rep_id":dr_id,
//									"date":dr_date,
//									"work_time":dr_work_time,
//									"over_time_hour":dr_ot_time,
//									"plan":dr_plan,
//									"proj_id":dr_proj,
//									"prp_id":dr_prp,
//									"task":dr_task,
//									"state":dr_state
//								},
								success: function(data) {
									var jsonData = eval("("+data+")");
									if(jsonData.messageType == "success"){
										alert_div($(".alert_container"),jsonData.message);
										ajax_position_search(dept_id,dept_name,position_id,position_name,position_type,position_maxmember,page_num,false);
									}
									else
										alert_div($(".alert_container"),jsonData.message);
									$(".modal-content").css("display", "none");
									$(".holyContainer").css("opacity", 1);
								}
							});
							
					}   
				});
				
			}
		});
	}
	
	var ajax_emp_search = function(position_id,page_num,refresh){
		$.ajax({
			// 发送方法
			type:"GET",
			// 发送url
			url:"fake/position-management/table-emp.JSON",
//			data:{
//			"service":"searchDrs",
//			"dept_id":dept_id,
//			"dept_name":dept_name,
//			"position_id":position_id,
//			"position_name":position_name,
//			"position_type":position_type,
//			"page_num":page_num
//			},
			// 成果返回后
			success: function(response) {
				console.log(response);
				var jsonData = eval("("+response+")");
				if(jsonData.total_num == "0"){
					alert_div($(".alert_container"),"没有员工信息");
					$(".emp_list_container").html("");
				}
				else{
					if(refresh){
						close_alert();
					}
					ajax_load_emp(response);
				}
			}
		});
	}
	
	var ajax_load_emp = function(emp_list_data){
		
		var jsonData = eval("("+emp_list_data+")");
		//获得列表显示
		var empInfoHtml = getEmpList(jsonData.tableRow);
		
		$(".emp_list_container").replaceWith(empInfoHtml);
		//设置table foot
		$(".page_num").text(jsonData.page_num);
		$(".total_page").text(jsonData.total_page);
		$(".total_num").text(jsonData.total_num);
		
	}
	
	var getEmpList = function(list){
		var html = "<tbody class='emp_list_body'> ";
						for(var i=0;i<list.length;i++){
							var temp_data = list[i];
							html+="<tr data-ng-repeat='row in tableRow'>" +
										"<td class='center' data-model-id='{{row.id}}>" +
											"<label class='pos-rel'>" +
												"<input type='checkbox' class='ace emp_check' value='"+temp_data.id+"'/>" +
												"<span class='lbl'></span>" +
											"</label>" +
										"</td>" +
										"<td>"+temp_data.emp_id+"</td>" +
										"<td>"+temp_data.emp_name+"</td>" +
									"</tr>";
						}
		html+="</tbody>";
		return html;
	}
	
	var alert_div = function(container,msg){
		container.css("display","none");
		container.html("");
		var div = "<div class='alert_div alert alert-success alert-dismissible fade in' role='alert'>" +
						"<button type='button' class='close' data-dismiss='alert' aria-label='Close'>" +
							"<span aria-hidden='true'>×</span>" +
						"</button>" +
						"<strong class='show_ms'></strong>" +
				"</div>";
		container.html(div);
		$(".show_ms").text(msg);
		container.css("display","");
	}
	
	var close_alert = function(){
		$(".alert_container").html("");
		$(".alert_container").css("display","none");
	}
});


